package com.booklove.controller.book;


import com.booklove.constant.JudgeStateConstant;
import com.booklove.context.BaseContext;
import com.booklove.domain.dto.DonatebookDTO;
import com.booklove.domain.dto.PageDTO;
import com.booklove.domain.po.Address;
import com.booklove.domain.po.Books;
import com.booklove.domain.po.Donatebook;
import com.booklove.domain.query.DonatebookQuery;
import com.booklove.domain.vo.DonatebookVO;
import com.booklove.exception.AddressSubmitException;
import com.booklove.exception.DonateBookCreateException;
import com.booklove.exception.DonateBookDeleteException;
import com.booklove.result.Result;
import com.booklove.service.delivery.AddressService;
import com.booklove.service.book.BooksService;
import com.booklove.service.book.DonatebookService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("donatebookCLL")
@RequestMapping("/book/donatebooks")
@RequiredArgsConstructor// 使用基本注解的注入形式，仅仅注入需要的变量
public class DonatebookController {

    private final DonatebookService donatebookService;
    private final BooksService booksService;
    private final AddressService addressService;


    /**
     * 保存新增
     */
    @PostMapping("/")
    Result<String> saveDonatebook(@RequestBody DonatebookDTO donatebookDTO) {
        donatebookService.createDonateBook(donatebookDTO);
        return Result.success();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/id")
    Result<String> deleteDonatebook(@RequestParam("id") Long id) {
        Donatebook donatebook = donatebookService.getById(id);
        if (donatebook == null) {
            throw new DonateBookDeleteException("非法的捐书删除请求");
        }
        if (donatebook.getUserId() != BaseContext.getCurrentId().intValue()) {
            throw new DonateBookDeleteException("非法的捐书删除请求");
        }


        // 删除关联书本
        booksService.removeById(donatebook.getBookId());
        // 删除此次捐献 id
        donatebookService.removeById(id);
        return Result.success();
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    DonatebookVO getDonatebook(@PathVariable Integer id) {
        return donatebookService.getVOById(id);
    }


    /**
     * 编辑数据
     *
     * @param donatebookDTO 实体
     * @return 编辑结果
     */
    @PutMapping("/")
    Result<String> updateDonatebook(@RequestBody DonatebookDTO donatebookDTO) {
        // 验证信息非空
        /*
        - 捐赠title
        - 捐赠发言recommend
        - 捐赠书籍名称name
         */
        if (donatebookDTO.getTitle() == null || donatebookDTO.getTitle().isBlank()
                || donatebookDTO.getName() == null || donatebookDTO.getName().isBlank()
                || donatebookDTO.getRecommend() == null || donatebookDTO.getRecommend().isBlank()
//                || donatebookDTO.getCover() == null || donatebookDTO.getCover().isBlank()
        ) {
            throw new DonateBookCreateException("书籍信息或是捐献信息缺失");
        }

        Integer addressId = donatebookDTO.getAddressId();
        Address address = addressService.getById(addressId);
        if (address == null || address.getUserId() != BaseContext.getCurrentId().intValue()) {
            throw new AddressSubmitException("地址信息非法，请检测内容");
        }


        // 需要放置bookId
        Donatebook donatebook = donatebookService.getById(donatebookDTO.getId());
        // 如果dto中有null字段，那么也会被覆盖，然后在update方法中，不会被执行更新
        //更新donate内容
        Donatebook updateDonatebook = new Donatebook();
        BeanUtils.copyProperties(donatebookDTO, updateDonatebook);
        donatebookService.updateById(updateDonatebook);

        // 获取书本id
        Integer bookId = donatebook.getBookId();

        //        更新书本
        Books book = new Books();
        BeanUtils.copyProperties(donatebookDTO, book);
        book.setId(bookId);
        booksService.updateById(book);

        return Result.success();
    }


    /**
     * 分页查询
     *
     * @param donatebookQuery 筛选条件
     *                        此筛选方法一定需要使用userId
     * @return 查询结果
     */
    @GetMapping("/page")
    PageDTO<DonatebookVO> donatebooksPageQuery(DonatebookQuery donatebookQuery) {
        donatebookQuery.setUserId(BaseContext.getCurrentId().intValue());
        return donatebookService.queryDonatebooksPage(donatebookQuery);
    }

    /**
     * 分页查询
     *
     * @param donatebookQuery 筛选条件
     *                        此筛选方法一定需要使用已经审核完毕的代码
     * @return 查询结果
     */
    @GetMapping("/list")
    PageDTO<DonatebookVO> donatebooksListQuery(DonatebookQuery donatebookQuery) {
        // 强制为获取审核通过的
        donatebookQuery.setJudgeState(JudgeStateConstant.ACCEPT);
        return donatebookService.queryDonatebooksPage(donatebookQuery);
    }

}

